package cn.blnp.net.flowable.boot.localize.task.skip;

import cn.blnp.net.flowable.boot.service.TaskCarbonCopyService;
import cn.blnp.net.flowable.boot.service.TaskRecallService;
import cn.hutool.core.map.MapUtil;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <h3>流程节点自动跳过用例</h3>
 *
 * @author <a href="mailto:blnp.yibin@qq.com">lyb</a>
 * @version 1.0
 * @since 2025/6/20 10:49
 */
@Slf4j
@SpringBootTest
public class AutoSkipProcessTest {

    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;
    @Resource
    private IdentityService identityService;
    @Resource
    private TaskService taskService;
    @Resource
    private HistoryService historyService;
    @Resource
    private TaskRecallService taskRecallService;
    @Resource
    private ManagementService managementService;

    /**
     *  <h2>任务知会案例演示</h2>
     *  <ul>
     *      <li><b>流程定义文件：</b>{@link flow/autoSkipProcess.bpmn20.xml}</li>
     *      <li>
     *          <b>流程预览图：</b></br>
     *          <img src="https://cdn4.winhlb.com/2025/06/20/6854cca92f0ca.png" >
     *      </li>
     *  </ul>
     **/
    @Test
    public void test() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("flow/autoSkipProcess.bpmn20.xml")
                .name("流程节点自动跳过案例")
                .deploy();
        log.info("部署ID：{}", deploy.getId());
        //流程定义信息
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .deploymentId(deploy.getId())
                .singleResult();
        log.info("流程定义ID：{}", processDefinition.getId());
        //初始化流程变量
        Map<String, Object> variables = MapUtil.builder(new HashMap<String, Object>())
                .put("employee", "安琪拉")
                .put("leader", "妲己")
                .put("manager", "妲己")
                .put("hr", "大乔")
                .put("_FLOWABLE_SKIP_EXPRESSION_ENABLED", true)
                .build();
        //启动流程实例
        ProcessInstance procInst = runtimeService.startProcessInstanceById(processDefinition.getId(), variables);
        log.info("流程实例ID：{}", procInst.getId());

        TaskQuery taskQuery = taskService.createTaskQuery()
                .processInstanceId(procInst.getId());
        //查询并完成第一个任务
        Task firstTask = taskQuery.singleResult();
        taskService.complete(firstTask.getId());
        log.info("用户任务{}办理完成。", firstTask.getName());
        //查询并完成第二个任务
        Task secondTask = taskQuery.singleResult();
        taskService.complete(secondTask.getId());
        log.info("用户任务{}办理完成。", secondTask.getName());
        //查询第三个任务
        Task thirdTask = taskQuery.singleResult();
        log.info("当前流程所处节点名称为：{}，节点key为：{}", thirdTask.getName(), thirdTask.getTaskDefinitionKey());

        /**
         *  演示结果：
         *  2025-06-20 11:01:32.173  INFO 23416 --- [           main] c.b.n.f.b.l.t.skip.AutoSkipProcessTest   : 部署ID：270001
         * 2025-06-20 11:01:32.179  INFO 23416 --- [           main] c.b.n.f.b.l.t.skip.AutoSkipProcessTest   : 流程定义ID：autoSkipProcess:2:270004
         * 2025-06-20 11:01:32.263  INFO 23416 --- [           main] c.b.n.f.b.l.t.skip.AutoSkipProcessTest   : 流程实例ID：270005
         * 2025-06-20 11:01:32.333  INFO 23416 --- [           main] c.b.n.f.b.l.t.skip.AutoSkipProcessTest   : 用户任务请假申请办理完成。
         * 2025-06-20 11:01:32.375  INFO 23416 --- [           main] c.b.n.f.b.l.t.skip.AutoSkipProcessTest   : 用户任务直属上级审批办理完成。
         * 2025-06-20 11:01:32.379  INFO 23416 --- [           main] c.b.n.f.b.l.t.skip.AutoSkipProcessTest   : 当前流程所处节点名称为：人力总监审批，节点key为：userTask4
         **/
    }
}
